package com.sh.data.engine.domain.base.model.entity;

import lombok.Builder;
import lombok.Data;

import java.lang.reflect.Field;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

@Data
@Builder
@Deprecated
public class FieldInfo {

    // 表名
    private String tableName;

    // 表注释
    private String tableComment;

    // 字段名
    private String fieldName;

    // 字段类型（从db中读取到的）
    private String fieldType;

    // 字段类型（hive中的字段类型）
    private String hiveType;

    // 字段类型（对应的java.sql.Types的SQL类型）
    private Integer dataType;

    // 字段长度
    private Integer columnSize;

    // 字段小数位长度
    private Integer decimalDigits;

    private String defaultValue;

    // 是否是分区字段
    private Integer isPartition;

    // 字段是否是主键
    private Integer isPk;

    // 字段可否为空 0-NO  1-YES
    private Integer isNullable;

    // 字段备注
    private String comment;

    // 字段排序
    private Integer integerIdx;

    public void updateDataType(Integer dataType) {
        this.dataType = dataType;
        this.fieldType = TYPE_MAP.getOrDefault(dataType, fieldType);
    }

    public void updateFieldType(String fieldType) {
        this.fieldType = fieldType;
        this.dataType = TYPE_MAP_REVERSED.getOrDefault(fieldType, Types.VARCHAR);
    }

    private static final Map<Integer, String> TYPE_MAP = new HashMap<>();
    private static final Map<String, Integer> TYPE_MAP_REVERSED = new HashMap<>();

    static {
        Field[] declaredFields = Types.class.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            try {
                TYPE_MAP.put(declaredField.getInt(Types.class), declaredField.getName());
                TYPE_MAP_REVERSED.put(declaredField.getName(), declaredField.getInt(Types.class));
            } catch (Exception ignore) {
            }
        }
    }

    @Override
    public String toString() {
        return "FieldInfoEntity [tableName="
            + tableName
            + ", tableComment="
            + tableComment
            + ", fieldName="
            + fieldName
            + ", fieldType="
            + fieldType
            + ", hiveType="
            + hiveType
            + ", dataType="
            + dataType
            + ", columnSize="
            + columnSize
            + ", decimalDigits="
            + decimalDigits
            + ", defaultValue="
            + defaultValue
            + ", isPartition="
            + isPartition
            + ", isPk="
            + isPk
            + ", isNullable="
            + isNullable
            + ", comment="
            + comment
            + ", integerIdx="
            + integerIdx
            + "]";
    }
}
